package by.bsu.scs.presentation.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import by.bsu.scs.util.manager.CookiesManager;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author Alexey Hursevich
 * @version 1.0
 * 
 * All actions connected with logging out.
 */
public class LogoutAction extends DispatchAction {
	
	private static final String LOGOUT = "logout";
	
	/**
	 * Responsible for logout of system users.
	 * <p>
	 * Also checks cookies to delete RememberMe parameter.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return mapping forward path
	 */
	public ActionForward exit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		SessionManager.getInstance().setUser(null, request);
		
		Cookie [] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie c: cookies) {
				if(CookiesManager.C_KEY_SAVE.equalsIgnoreCase(c.getName())) {
					c.setValue("false");
					response.addCookie(c);
				} 
			}
		}
		return mapping.findForward(LOGOUT);
	}
}
